package com.demomaster.quickcompat.widget.dialog;

import static android.view.View.OVER_SCROLL_NEVER;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.demomaster.quickcompat.R;
import com.demomaster.quickcompat.listener.OnSingleItemClickListener;
import com.demomaster.quickcompat.widget.dialog.adapter.SheetAdapter;

import java.util.Arrays;
import java.util.List;


/**
 * 多选
 * @author squirrel桓
 * @date 2019/1/7.
 * description：
 */
public class QuickShareDialog extends QuickDialog {
    private List<String> data;
    private int columnCount = 1;
    private ShowType showType = ShowType.List;
    private OnDialogActionListener onDialogActionListener;
    private int orientation;

    public QuickShareDialog(Context context, Builder builder) {
        super(context,builder);
        data = builder.data;
        columnCount = builder.columnCount;
        showType = builder.showType;
        //onDialogActionListener = builder.onDialogActionListener;
        orientation = builder.orientation;
        init();
    }

    private void init() {
        setCanceledOnTouchOutside(true);
        setCancelable(true);
        initData();
    }

    SheetAdapter sheetAdapter;
    public void setSheetAdapter(SheetAdapter sheetAdapter) {
        this.sheetAdapter = sheetAdapter;
        if(recyclerView!=null){
            recyclerView.setAdapter(sheetAdapter);
        }
    }

    RecyclerView recyclerView;
    private void initData() {
        recyclerView = findViewById(R.id.recycler);
        recyclerView.setOverScrollMode(OVER_SCROLL_NEVER);
        switch (showType) {
            case List:
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
                linearLayoutManager.setAutoMeasureEnabled(true);
                linearLayoutManager.setOrientation(orientation);
                recyclerView.setLayoutManager(linearLayoutManager);
                break;
            case Grid:
                GridLayoutManager gridLayoutManager =  new GridLayoutManager(getContext(), columnCount);
                gridLayoutManager.setOrientation(orientation);
                recyclerView.setLayoutManager(gridLayoutManager);
                break;
        }

        if(sheetAdapter==null) {
            sheetAdapter = new SheetAdapter(getContext(), data);
        }
        if(sheetAdapter.getOnItemClickListener() == null){
            sheetAdapter.setOnItemClickListener(new OnSingleItemClickListener() {
                @Override
                public void onItemClickEvent(AdapterView<?> parent, View view, int position, long id) {
                    onDialogActionListener.onItemClick(QuickShareDialog.this, position, data);
                }
            });
        }
        recyclerView.setAdapter(sheetAdapter);
        //setPanelMaginTop(DisplayUtil.getStatusBarHeight(getContext()) + DisplayUtil.getActionBarHeight(getContext()));
    }

    @Override
    public boolean isCanSliding() {
        return true;
    }

    public enum ShowType {
        List, Grid
    }

    public static class Builder extends QuickDialog.Builder<Builder>{
        private List<String> data;
        private int columnCount = 1;
        private ShowType showType = ShowType.List;
        private int orientation = LinearLayout.VERTICAL;
        public Builder(Context context) {
            super(context);
            animationStyleID = R.style.keybored_anim;
            isFullScreen = true;
            enablePadding = false;
            setContentView(R.layout.quick_dialog_share_default);
        }

        public Builder setData(List<String> data) {
            this.data = data;
            return this;
        }

        public Builder setData(String[] data) {
            this.data = Arrays.asList(data);
            return this;
        }

        public Builder setShowType(ShowType showType) {
            this.showType = showType;
            return this;
        }

        public Builder setOrientation(int orientation) {
            this.orientation = orientation;
            return this;
        }

        public QuickShareDialog create() {
            return new QuickShareDialog(context, this);
        }
    }

    public interface OnDialogActionListener {
        void onItemClick(QuickShareDialog dialog, int position, List<String> data);
    }
}
